﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace StyleMVVM.DependencyInjection.Conditions
{
	public sealed class WhenTargetHas : IExportCondition
	{
		private Type attributeType;

		public WhenTargetHas(Type attributeType)
		{
			this.attributeType = attributeType;
		}

		public bool ConditionMeet(IDependencyInjectionContainer container,
		                          IInjectionContext injectionContext,
		                          IExportStrategy exportStrategy)
		{
			IInjectionTargetInfo target = injectionContext.Target;

			if (target != null)
			{
				foreach (object attr in target.InjectionTargetAttributes)
				{
					if (attributeType.GetTypeInfo().IsAssignableFrom(attr.GetType().GetTypeInfo()))
					{
						return true;
					}
				}
			}

			return false;
		}
	}
}